package com.qidian.hw.servlet;

import com.qidian.hw.pojo.SolutionType;
import com.qidian.hw.service.SolutionTypeService;
import com.qidian.hw.service.impl.SolutionTypeServiceImpl;
import com.qidian.hw.tools.Const;
import com.qidian.hw.tools.FormatUtil;
import com.qidian.hw.tools.JSONTools;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @Author ꒰ঌ(李ᆺ琛)໒꒱
 * @Date 2022/8/19 20:13
 * @Version 1.8
 */
public class SolutionTypeServlet extends HttpServlet {
    private final SolutionTypeService solutionTypeService = new SolutionTypeServiceImpl ();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 处理POST请求的中文乱码问题
        if (req.getMethod ().equalsIgnoreCase ("POST")) {
            req.setCharacterEncoding ("utf-8");
        }
        String op = req.getParameter ("op");
        switch (op) {
            // 保存数据
            case "save":
                save (req, resp);
                break;
            // 回显数据
            case "input":
                input (req, resp);
                break;
            // 修改数据
            case "update":
                update (req, resp);
                break;
            // 显示数据列表
            case "list":
                list (req, resp);
                break;
            // 删除数据
            case "delete":
                delete (req, resp);
                break;
            //批量删除
            case "deleteBatch":
                deleteBatch (req, resp);
                break;
            default:
                super.service (req, resp);
        }
    }

    private void deleteBatch(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int result = 0;
        String[] ids = req.getParameterValues ("solutionTypeId");
        if (ids != null && ids.length > 0) {
            result = solutionTypeService.deleteBatch (ids);
        }
        resp.setContentType (Const.RES_HTML);
        if (result > 0) {
            resp.getWriter ().write ("删除成功！");
        } else resp.getWriter ().write ("删除失败！");
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ArrayList<SolutionType> solutionTypes = solutionTypeService.listSolutionType ();
        String jsons = JSONTools.object2json (solutionTypes);
        resp.setContentType (Const.RES_JSON);
        resp.getWriter ().write (jsons);
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Long solutionTypeId = 0L;
        try {
            solutionTypeId = Long.parseLong (req.getParameter ("solutionTypeId"));
        } catch (NumberFormatException e) {
            e.printStackTrace ();
        }
        int result = solutionTypeService.deleteSolutionType (solutionTypeId);
        resp.setContentType (Const.RES_HTML);
        if (result == 1) {
            resp.getWriter ().write ("删除成功！");
        } else resp.getWriter ().write ("删除失败！");
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String solutionTypeTitle = req.getParameter ("solutionTypeTitle");
        Long solutionTypeId = Long.parseLong (req.getParameter ("solutionTypeId"));
        SolutionType solutionType = new SolutionType ();
        solutionType.setSolutionTypeId (solutionTypeId);
        solutionType.setModifyTime (FormatUtil.getNow ());
        solutionType.setSolutionTypeTitle (solutionTypeTitle);
        int result = solutionTypeService.updateSolutionType (solutionType);
        resp.setContentType (Const.RES_HTML);
        if (result == 1) {
            resp.getWriter ().write ("更新成功！");
        } else resp.getWriter ().write ("更新失败！");
    }

    private void input(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Long solutionTypeId = 0L;
        try {
            solutionTypeId = Long.parseLong (req.getParameter ("solutionTypeId"));
        } catch (NumberFormatException e) {
            e.printStackTrace ();
        }
        SolutionType solutionType = solutionTypeService.querySolutionTypeById (solutionTypeId);
        String json = JSONTools.object2json (solutionType);
        resp.setContentType (Const.RES_JSON);
        resp.getWriter ().write (json);
    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String solutionTypeTitle = req.getParameter ("solutionTypeTitle");
        SolutionType solutionType = new SolutionType ();
        solutionType.setSolutionTypeTitle (solutionTypeTitle);
        int result = solutionTypeService.saveSolutionType (solutionType);
        resp.setContentType (Const.RES_HTML);
        if (result == 1) {
            resp.getWriter ().write ("添加成功！");
        } else resp.getWriter ().write ("添加失败！");
    }
}
